@echo off
echo ====================================
echo   Build script for the admin system
echo ====================================

REM Check Node.js version
node -v > nul 2>&1
if %ERRORLEVEL% NEQ 0 (
  echo Error: Node.js not installed!
  exit /b 1
)
echo Node.js version: 
node -v

REM Check npm version
echo npm version: 
npm -v

REM Clean old build files
echo Cleaning old build files...
if exist dist (
  rmdir /s /q dist
  echo Dist directory cleaned
)

REM Install dependencies
echo Installing dependencies...
call npm install

if %ERRORLEVEL% NEQ 0 (
  echo Dependency installation failed
  exit /b 1
)
echo Dependencies installed

REM Build project
echo Building project...
call npm run build

if %ERRORLEVEL% NEQ 0 (
  echo Build failed
  exit /b 1
)

REM Check build result
if not exist dist (
  echo Dist directory doesn't exist after build!
  exit /b 1
)

echo Build successful!
echo Build files located at: %CD%\dist\

REM Create additional files for better file:// protocol support
echo Creating helper files for direct file access...
echo ^<!DOCTYPE html^>^
^<html^>^
^<head^>^
  ^<meta charset="UTF-8"^>^
  ^<meta http-equiv="refresh" content="0;url=index.html#/"^>^
  ^<title^>重定向中...^</title^>^
^</head^>^
^<body^>^
  ^<p^>如果页面没有自动跳转，请^<a href="index.html#/"^>点击这里^</a^>继续。^</p^>^
  ^<script^>^
    window.location.href = 'index.html#/';^
  ^</script^>^
^</body^>^
^</html^> > dist\redirect.html

copy dist\index.html dist\backup-index.html
echo Backup of index.html created as backup-index.html

echo ====================================
echo Frontend build completed
echo ====================================

REM Next steps
echo Next steps:
echo 1. Deploy files from the dist directory to a web server
echo 2. Configure Nginx or other web server to point to these static files
echo 3. Ensure API requests are correctly proxied to the backend service
echo 4. If testing locally, use 'start-local-server.bat' to start a test server

pause 